package com.ellricky.baselib.ui.student

import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.andr.common.http.callback.JOCallback
import com.andr.common.permission.PermissionManager
import com.andr.common.permission.Permissions
import com.andr.common.util.DialUtil
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.new.StuDetail
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.listener.OnTakePhotoListener
import com.ellricky.baselib.ui.StudentEditActivity
import com.ellricky.baselib.ui.TrackActivity
import com.ellricky.baselib.ui.base.BaseDetailActivity
import com.ellricky.baselib.ui.order.CustomFragmentProviderActivity
import com.ellricky.baselib.ui.xufei.PayHistoryActivity
import com.ellricky.baselib.ui.xufei.PayZjsDetailActivity
import com.ellricky.baselib.ui.xufei.PriceAddActivity
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.EDialogUtil
import com.ellricky.baselib.util.NewECallback
import com.ellricky.baselib.util.jump
import kotlinx.android.synthetic.main.base_activity_stu_detail.*
import org.json.JSONObject
import java.util.*

class StudentDetailActivity : BaseDetailActivity(), OnTakePhotoListener {

    private var ePickerUtil: EPickerUtil? = null
    private var detailEntity: StuDetail? = null
    private var dialType = 0
    private val TOSTU = 0
    private val TOMOM = 1
    private val TODAD = 2
    private var needPay = false

    override fun getContentLayout(): Int {
        return R.layout.base_activity_stu_detail
    }

    override fun initUIView() {
        super.initUIView()
        ePickerUtil = EPickerUtil(this)
        mTvTitle.setText(R.string.base_stu_detail)
        val frgType = intent.getStringExtra(BaseConstant.KEY_PAGE_TYPE)
        if (LocalCacheUtil.isPresidentRole() || LocalCacheUtil.isTmRole()) {
//            setRightBtn(0)
        } else if (LocalCacheUtil.isConsultantRole() && BaseConstant.CLUE_TYPE_TY == frgType) {
            needPay = true
            mBtnRight.visibility = View.VISIBLE
            mBtnRight.text = "缴费"
        }

        tvPaySeeDetail.setOnClickListener(this)
        ivDadPhone.setOnClickListener(this)
        ivMomPhone.setOnClickListener(this)
        tvPersonSeeDetail.setOnClickListener(this)
        btnClassRecord.setOnClickListener(this)
        btnPhoto.setOnClickListener(this)
        btnPriceAdd.setOnClickListener(this)
        tvTrackSeeDetail.setOnClickListener(this)
        ivEdit.setOnClickListener(this)
        tvConsultantEdit.setOnClickListener(this)
        tvTmEdit.setOnClickListener(this)
        tvFormalEdit.setOnClickListener(this)
        btnIntegralRecord.setOnClickListener(this)
        btnClassConsume.setOnClickListener(this)
        btnVisitRecord.setOnClickListener(this)

        if (LocalCacheUtil.isTcRole()) {
            btnPriceAdd.visibility = View.GONE
        }
        initViewWithCardType()
    }

    private fun initViewWithCardType() {
        when (LocalCacheUtil.getCardType()) {
            BaseConstant.CARD_TEACHER -> {
                ivEdit.visibility = View.GONE
                btnPriceAdd.visibility = View.GONE
            }
            BaseConstant.CARD_PRESIDENT -> {
                tvConsultantEdit.visibility = View.VISIBLE
                tvTmEdit.visibility = View.VISIBLE
                tvFormalEdit.visibility = View.VISIBLE
            }
            BaseConstant.CARD_CONSULTANT -> {
                tvConsultantEdit.visibility = View.VISIBLE
                tvTmEdit.visibility = View.VISIBLE
                tvFormalEdit.visibility = View.VISIBLE
            }
        }
    }

    override fun onResume() {
        super.onResume()
        loadDetail(id)
    }

    private fun loadDetail(id: String) {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = getActions()
        paramMap["member_id"] = id
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<StuDetail?>(this) {
            override fun onSuccess(data: StuDetail?, msg: String?) {
                data?.also {
                    detailEntity = it
                    tvStuName.text = data.memeber_name
                    tvAge.text = data.memeber_sex + " " + data.member_age
                    tvSchoolArea.value = data.school_name
                    tvSchool.value = data.member_school
                    tvBirthDay.value = data.member_birthday
                    tvBaseHour.value = data.basic_num
                    tvSpecialHour.value = data.art_num
                    tvArtHour.value = data.special_num
                    tvGiveHour.value = data.give_num
                    tvDadPhone.value = data.father_tel
                    tvMomPhone.value = data.mother_tel
                    tvDadWork.value = data.father_work
                    tvMomWork.value = data.mother_work
                    tvHomeAddr.value = data.address
                    tvConsultant.value = data.sale_name
                    tvTy.value = data.teacher_name
                    tvTm.value = data.gza_name
                    tvFormal.value = data.teacher_name2
                    tvZjsPerson.value = data.introduction_num
                    tvRemark.value = data.remark
                }
            }
        })
    }

    private fun getActions(): String {
        return if (LocalCacheUtil.getCardType() == BaseConstant.CARD_PRESIDENT) {
            "headStudentDetail"
        } else {
            "studentDetail"
        }
    }

    override fun onClickBtnRight() {
        super.onClickBtnRight()
        when {
            needPay -> {
                EAppUtil.jumpPageWithId(this, PriceAddActivity::class.java, id)
            }
            detailEntity != null -> EDialogUtil.showStuDetailDialog(
                this,
                this
            )
            else -> ToastUtil.show(this, "详情信息为空，不能进行操作！")
        }
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        if (v === tvPaySeeDetail) {
            jump<PayHistoryActivity> {
                putExtra(BaseConstant.KEY_ID, id)
            }
        } else if (v === ivDadPhone) {
            dialType = TODAD
            dial()
        } else if (v === ivMomPhone) {
            dialType = TOMOM
            dial()
        } else if (v === tvPersonSeeDetail) {
            EAppUtil.jumpPageWithId(this, PayZjsDetailActivity::class.java, id)
        } else if (v === btnPhoto) {
            jump<StudentPhotoActivity> {
                putExtra(BaseConstant.KEY_ID, id)
            }
        } else if (v === btnPriceAdd) {
            jump<PriceAddActivity> {
                putExtra(BaseConstant.KEY_ID, id)
                putExtra(BaseConstant.KEY_FROM, "student")
            }
        } else if (v === btnClassRecord) {
            jump<StudentClassRecordActivity> {
                putExtra(BaseConstant.KEY_ID, id)
            }
        } else if (v == tvTrackSeeDetail) {
            jump<TrackActivity> {
                putExtra(BaseConstant.KEY_ID, id)
            }
        } else if (v == ivEdit) {
            EAppUtil.jumpPageWithId(this, StudentEditActivity::class.java, id)
        } else if (v == tvConsultantEdit) {
            jump<StudentTeacherEditActivity> {
                putExtra(BaseConstant.KEY_ID, id)
                putExtra(BaseConstant.KEY_TYPE, "consultant")
            }
        } else if (v == tvTmEdit) {
            jump<StudentTeacherEditActivity> {
                putExtra(BaseConstant.KEY_ID, id)
                putExtra(BaseConstant.KEY_TYPE, "teacherManager")
            }
        } else if (v == tvFormalEdit) {
            jump<StudentTeacherEditActivity> {
                putExtra(BaseConstant.KEY_ID, id)
                putExtra(BaseConstant.KEY_TYPE, "teacher")
            }
        } else if (v == btnIntegralRecord) {
            val bundle = Bundle().apply {
                putString(BaseConstant.KEY_ID, id)
            }
            EAppUtil.jumpCommonFragment(this, BaseConstant.FRAGMENT_CLASS_INTEGRAL, bundle)
        } else if (v == btnClassConsume) {
            val bundle = Bundle().apply {
                putString(BaseConstant.KEY_ID, id)
            }
            EAppUtil.jumpCommonFragment(this, BaseConstant.FRAGMENT_CLASS_CONSUME_RECORD, bundle)
        }else if (v == btnVisitRecord) {
            val bundle = Bundle().apply {
                putString(BaseConstant.KEY_ID, id)
            }
            EAppUtil.jumpCommonFragment(this, BaseConstant.FRAGMENT_CLASS_VISIT_LIST, bundle)
        }
    }

    override fun onClickItem(dialogInterface: DialogInterface, item: Int) {
//        if (item == 0) {
//            var className: String? = ""
//            if (detailEntity != null) className = detailEntity!!.className
//            if (EmptyUtil.isEmpty(className)) {
//                // 无班级
//                EAppUtil.jumpClassManage(this)
//            } else {
//                // 有班级
//                ePickerUtil!!.checkClassPicker(null) { pickType,
//                                                       t ->
//                    changeClass(t?.id ?: "")
//                }
//            }
//        } else if (item == 1) {
//            EAppUtil.jumpPageWithId(this, PriceAddActivity::class.java, id)
//        } else if (item == 2) {
//            // 电话回访
//            dialType = TOSTU
//            dial()
//        }
    }

    private fun dial() {
        PermissionManager.getInstance()
            .setGrantResult(this)
            .requestPermissions(this, Permissions.CALL_PHONE)
    }

    override fun onGrantedAll() {
        super.onGrantedAll()
        if (detailEntity != null) {
            var tel = detailEntity!!.father_tel
            if (dialType == TOMOM) {
                tel = detailEntity!!.mother_tel
            } else if (dialType == TODAD) {
                tel = detailEntity!!.father_tel
            }
            if (EmptyUtil.isEmpty(tel)) {
                ToastUtil.show(this, "电话号码为空，无法拨打")
                return
            }
            DialUtil.dial(this, tel)
        }
    }

    override fun onDenied(vararg permission: String) {
        super.onDenied(*permission)
        if (permission[0] == Permissions.CALL_PHONE) {
            ToastUtil.show(this, "请打开打电话权限")
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == BaseConstant.RESULT_CODE_ADD) {
            loadDetail(id)
        }
    }
}